package com.yqg.protection.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yqg.protection.common.Result;
import com.yqg.protection.common.interceptor.SysLog;
import com.yqg.protection.entity.FireLaws;
import com.yqg.protection.service.IFireLawsService;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * FireLawsController 前端控制器
 * @author zxf
 * @since 2024-09-11
 */
@RestController
@RequestMapping("/fire-laws")
public class FireLawsController {

    @Resource
    private IFireLawsService fireLawsService;

    /**
    *  新增或者更新
    */
    @PostMapping("/edit")
    @SysLog("新增或者修改消防法规")
    public Result save(@RequestBody FireLaws fireLaws) {
        fireLaws.setIsDelete(0);
        return Result.success(fireLawsService.saveOrUpdate(fireLaws));
    }
    /**
    *  删除
    */
    @GetMapping("delete/{id}")
    @SysLog("删除消防法规")
    public Result delete(@PathVariable Integer id) {
        FireLaws fireLaws =fireLawsService.getById(id);
        fireLaws.setIsDelete(1);
        return Result.success(fireLawsService.saveOrUpdate(fireLaws));
    }
    /**
    *  查询所有
    */
    @GetMapping("/all")
    public Result findAll() {
        QueryWrapper<FireLaws> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_delete", 0);
       List<FireLaws> list=  fireLawsService.list(queryWrapper);
        return Result.success(list);
    }
    /**
    *  根据id查询
    */
    @GetMapping("/{id}")
    public Result findOne(@PathVariable Integer id) {
        return Result.success(fireLawsService.getById(id));
    }
    /**
    *  分页查询
    */
    @GetMapping("/page")
    public Result findPage(@RequestParam Integer pageNum,
                           @RequestParam Integer pageSize,
                           String keyWord,
                           String enactStartTime,
                           String enactEndTime,
                           String materialStartTime,
                           String materialEndTime) {
        QueryWrapper<FireLaws> queryWrapper = new QueryWrapper<>();
        if (enactStartTime!=null && !enactStartTime.equals("")) {
            queryWrapper.between("enactment_date", enactStartTime, enactEndTime);
        }
        if (materialStartTime!=null && !materialStartTime.equals("")) {
            queryWrapper.between("material_date", materialStartTime, materialEndTime);
        }
        queryWrapper.and(qw -> qw.like("laws_name", keyWord).or().like("promulgat_author",keyWord).or().like("promulgat_number",keyWord));
        queryWrapper.eq("is_delete", 0);
        queryWrapper.orderByDesc("id");
        return Result.success(fireLawsService.page(new Page<>(pageNum, pageSize), queryWrapper));
    }

    @ApiOperation("导出数据")
    @GetMapping("/export")
    public void export(HttpServletResponse response,
                       String keyWord,
                       String enactStartTime,
                       String enactEndTime,
                       String materialStartTime,
                       String materialEndTime) {
        fireLawsService.export(response, keyWord, enactStartTime, enactEndTime, materialStartTime, materialEndTime);
    }

    @ApiOperation("导入数据")
    @PostMapping("/importData")
    @SysLog("导入消防法规")
    public void importData(MultipartFile file) {
        fireLawsService.importData(file);
    }
}

